﻿using System;
using System.Collections.Generic;
using Framework.Dependency;
using Interfaces.Criteria;
using Interfaces.Filters;
using Interfaces.Models;
using Interfaces.Providers;
using Microsoft.Practices.Unity;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using YQLDataProvider;
using YQLDataProvider.Providers;

namespace DataProviderIntegrationTests.ProvidersTests
{
    [TestClass]
    public class YqlIndustrySectorsProviderTests
    {
        [ClassInitialize]
        public static void InitTests(TestContext context)
        {
           var module = new YqlDataProviderModule(DependencyManager.Container);
            module.Initialize();
        }

        [TestMethod]
        public void GetDataTest_IsLazyLoadingTrue_OnlySectorDataReturned()
        {
            YqlIndustryProviderTetsTemplate(given: (baseCriteria, filter) =>
                {
                    baseCriteria.Setup(x => x.IsLazyLoading).Returns(true);
                    baseCriteria.Setup(x => x.IsFullLoading).Returns(false);
                    filter.Setup(x => x.Filtrate(It.IsAny<IEnumerable<object>>())).Returns((IEnumerable<object> y) => y);
                },
                                            when: (provider, criteria, filter) => provider.GetData(criteria, filter),
                                                then: result =>
                                                    {
                                                        Assert.IsNotNull(result);
                                                        foreach (var industrySector in result)
                                                        {
                                                            Assert.IsNotNull(industrySector.Id);
                                                            Assert.IsNotNull(industrySector.Name);
                                                            Assert.IsNull(industrySector.Industries);
                                                        }
                                                    });
        }

        [TestMethod]
        [Ignore]
        public void GetDataTest_IsFullLoadingTrue_SectorIndustriesWithCompanyDataReturned()
        {
            YqlIndustryProviderTetsTemplate(given: (baseCriteria, filter) =>
            {
                baseCriteria.Setup(x => x.IsLazyLoading).Returns(false);
                baseCriteria.Setup(x => x.IsFullLoading).Returns(true);
                filter.Setup(x => x.Filtrate(It.IsAny<IEnumerable<object>>())).Returns((IEnumerable<object> y) => y);
            },
                                            when: (provider, criteria, filter) => provider.GetData(criteria, filter),
                                                then: result =>
                                                {
                                                    Assert.IsNotNull(result);
                                                    foreach (var industrySector in result)
                                                    {
                                                        Assert.IsNotNull(industrySector.Id);
                                                        Assert.IsNotNull(industrySector.Name);
                                                        Assert.IsNotNull(industrySector.Industries);
                                                        foreach (var ind in industrySector.Industries)
                                                        {
                                                            Assert.IsNotNull(ind.Companies);
                                                            Assert.IsNotNull(ind.Sector);
                                                        }
                                                    }
                                                });
        }

        [TestMethod]
        public void GetDataTest_PartDataRetrned_SectorIndustriesWithCompanyDataReturned()
        {
            YqlIndustryProviderTetsTemplate(given: (baseCriteria, filter) =>
            {
                baseCriteria.Setup(x => x.IsLazyLoading).Returns(false);
                baseCriteria.Setup(x => x.IsFullLoading).Returns(false);
                filter.Setup(x => x.Filtrate(It.IsAny<IEnumerable<object>>())).Returns((IEnumerable<object> y) => y);
            },
                                           when: (provider, criteria, filter) => provider.GetData(criteria, filter),
                                               then: result =>
                                               {
                                                   Assert.IsNotNull(result);
                                                   foreach (var industrySector in result)
                                                   {
                                                       Assert.IsNotNull(industrySector.Id);
                                                       Assert.IsNotNull(industrySector.Name);
                                                       Assert.IsNotNull(industrySector.Industries);
                                                       foreach (var ind in industrySector.Industries)
                                                       {
                                                           Assert.IsNull(ind.Companies);
                                                           Assert.IsNotNull(ind.Sector);
                                                       }
                                                   }
                                               });
        }

        private static void YqlIndustryProviderTetsTemplate(Action<Mock<IBaseCriteria>, Mock<ISectorsFilter>> given,
                                                     Func< YqlIndustrySectorsDataProvider, IBaseCriteria, ISectorsFilter,IEnumerable<IIndustrySector>> when,
                                                     Action<IEnumerable<IIndustrySector>> then)
        {
            var provider = new YqlIndustrySectorsDataProvider();
            var baseCriteria = new Mock<IBaseCriteria>();
            var filter = new Mock<ISectorsFilter>();
            given(baseCriteria, filter);
            var result = when(provider, baseCriteria.Object, filter.Object);
            then(result);
        }
    }
}
